package com.b2c.ftysys.helper;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 随机数工具类
 * @author Administrator
 *
 */
public class RandomNumberUtils {
	
	private static String getTimeCode(String customCode) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
		String strDateTime=sdf.format(new Date());
		String d = (String.valueOf(Math.random())).substring(2, 8);
		if(customCode!=null&&customCode!="")
		    return strDateTime+"_"+d+"_"+customCode;
		else
			return strDateTime+"_"+d;
	}

	/**
	 * getPk,获得唯一字符串，同一微秒内300000个不会重复
	 * 
	 * @return long
	 */
	private static String[] ls = new String[300000];
	private static int li = 0;

	public synchronized static String getPk() {
		String lo = getTimeCode("");
		for (int i = 0; i < 300000; i++) {
			String lt = ls[i];
			if (lt == lo) {
				lo = getPk();
				break;
			}
		}
		ls[li] = lo;
		li++;
		if (li == 300000) {
			li = 0;
		}
		return lo;
	}
	
	/**
	 * 
	 * 方法描述：
	 * 获取int类型的随机码，9位
	 * 创建时间：2010-6-1 上午11:32:29 
	 * @return
	 */
	public synchronized static int getIntPk(){
		String a = (String.valueOf(System.currentTimeMillis())).substring(2, 8);
		String d = (String.valueOf(Math.random())).substring(2, 5);
		return Integer.parseInt((a+d));
	}
	
	public static void main(String[] args) {
		System.out.println(getPk());
		
	}
}
